package cn.hairui.fastms.core.controller;


import cn.hairui.fastms.base.cache.CacheManage;
import cn.hairui.fastms.base.config.WebLogInfo;
import cn.hairui.fastms.constant.FieldConstant;
import cn.hairui.fastms.constant.GlobalConstant;
import cn.hairui.fastms.core.annotation.TableResult;
import cn.hairui.fastms.core.bean.SConfig;
import cn.hairui.fastms.core.bean.SUser;
import cn.hairui.fastms.core.service.SConfigService;
import cn.hairui.fastms.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 系统参数配置 前端控制器
 * </p>
 *
 * @author lihairui
 * @since 2020-12-30
 */
@Controller
@RequestMapping("/sConfig")
public class SConfigController {
    @Autowired
    private SConfigService sConfigService;

    @WebLogInfo(description = "系统参数配置列表页面")
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request) {
        return "pages/core/sconfig/sconfig";
    }

    @WebLogInfo(description = "系统参数配置数据获取")
    @RequestMapping(value = "/data")
    @ResponseBody
    public TableResult data(HttpServletRequest request) {
        String pageSizeStr = request.getParameter("rows");
        String pageNumStr = request.getParameter("page");
        Integer pageNum = null;
        int pageSize = GlobalConstant.DEFAULT_PAGE_SIZE;
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSizeStr != null) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PageHelper.startPage(pageNum, pageSize);

        List<SConfig> sConfigList = sConfigService.list();
        PageInfo<SConfig> pageInfo = new PageInfo<>(sConfigList);
        return new TableResult(pageInfo.getTotal(), sConfigList);
    }

    @WebLogInfo(description = "系统参数配置数据保存")
    @RequestMapping(value = "/save")
    @ResponseBody
    public String save(HttpServletRequest request, SConfig sConfig, String flag) {
        SUser currentUser = (SUser) request.getSession().getAttribute(GlobalConstant.GLOBAL_SESSION_NAME);
        if (GlobalConstant.OPERATION_ADD.equals(flag)) {
            //新增保存
            //判断是否已经存在
            QueryWrapper<SConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(FieldConstant.CFG_KEY[0], sConfig.getCfgKey());

            if (sConfigService.list(queryWrapper).size() > 0) {
                //已存在不允许新增
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0003).toJSONString();
            } else {
                //不存在用户 允许新增
                sConfig.setUpdateTime(DateUtil.getCurrentDateTimeStr());
                sConfig.setUpdateUser(currentUser.getUserAccount());
                sConfigService.save(sConfig);
                //重载配置缓存
                CacheManage.initConfigMap(sConfigService);
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
            }

        } else if (GlobalConstant.OPERATION_EDIT.equals(flag)) {
            //更新保存
            sConfig.setUpdateTime(DateUtil.getCurrentDateTimeStr());
            sConfig.setUpdateUser(currentUser.getUserAccount());
            boolean result = sConfigService.saveOrUpdate(sConfig);
            if (result) {
                //更新成功
                //重载配置缓存
                CacheManage.initConfigMap(sConfigService);
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
            } else {
                //更新失败
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0001).toJSONString();
            }
        } else {
            return null;
        }
    }

    @WebLogInfo(description = "系统参数配置数据删除")
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Transactional
    public String delete(HttpServletRequest request, SConfig sConfig) {
        if (sConfig != null) {
            sConfig = sConfigService.getById(sConfig.getCfgId());
            String cfgKey = sConfig.getCfgKey();
            if (cfgKey.startsWith("cfgSystem")) {
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0004).toJSONString();
            }
        }
        sConfigService.removeById(sConfig.getCfgId());
        return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
    }
}

